<?php

namespace App\Console\Commands;

use App\Common\SDK\Facade\FenZhangTong;
use App\Models\AgentTopUpOrder;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Redis;

class GetFZTPaymentOrderStatusByOrderCreatedAt extends Command
{
    /**
     * 命令名称及签名
     *
     * @var string
     */
    protected $signature = 'utils:get_fzt_payment_order_status_by_order_created_at {created_at_begin} {created_at_end}';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = '查询时间范围内的订单银行支付状态';

    public function handle()
    {
        $batchNumber = implode('_', [
            Carbon::parse($this->argument('created_at_begin'))->format('YmdHis'),
            Carbon::parse($this->argument('created_at_end'))->format('YmdHis')
        ]);
        $this->output->text('操作批次号' . $batchNumber);
        AgentTopUpOrder::query()
            ->with(['agent:id,name,real_name,phone'])
            ->whereBetween('created_at', [$this->argument('created_at_begin'), $this->argument('created_at_end')])
            ->select(['order_id', 'status', 'agent_id'])
            ->each(function ($item) use ($batchNumber) {
                $item = $item->toArray();
                $this->output->text('查询处理: ' . $item['order_id']);
                $paymentOrderDetail = FenZhangTong::getPaymentOrderDetail(['platPayId' => $item['order_id']]);

                Arr::forget($paymentOrderDetail, 'signData');
                Redis::rpush('GET_FZT_PAYMENT_ORDER_STATUS_LIST:' . $batchNumber, json_encode(array_merge(
                    $paymentOrderDetail,
                    Arr::only($item, ['order_id', 'status', 'agent_id']),
                    [
                        'agent_name' => $item['agent']['name'],
                        'agent_real_name' => $item['agent']['real_name'],
                        'agent_phone' => $item['agent']['phone'],
                    ])));
                return true;
            });
    }
}
